// 1.请实现一个函数：最长顺子；输入很多个整数(1<=数值<=13)，返回其中可能组成的最长的一个顺子(顺子中数的个数代表顺的长度)； 
// 其中数字1也可以代表14； 顺子包括单顺\双顺\3顺；
// 单顺的定义是连续5个及以上连续的数，比如1,2,3,4,5、3,4,5,6,7,8和10,11,12,13,1等；
// 双顺的定义是连续3个及以上连续的对(对:两个相同的数被称为对),比如1,1,2,2,3,3、4,4,5,5,6,6,7,7和11,11,12,12,13,13,1,1等;
// 3顺的定义是连续2个及以上连续的3张(3张:3个相同的数被称为3张),比如1,1,1,2,2,2、3,3,3,4,4,4,5,5,5,6,6,6和13,13,13,1,1,1等等;
// 比如：输入数组[*********************,3,3,4], 输出数组[2,2,3,3,4,4,5,5]


namespace Sz {
    function getSz(arr:Array<number>):Array<number> | boolean{
        let mp:Map<number,number> = new Map();
        for(let i = arr.length-1;i>=0;i--){
            if(!mp.has(arr[i])){
                mp.set(arr[i],1)
            }else {
                mp.set(arr[i],mp.get(arr[i]) as number +1)
            }
        }
        let keyArr = Array.from(mp.keys()).sort((a,b)=>a-b);
        let xz1:Array<Array<number>> = [Array.of(keyArr[0])];
        let xz2:Array<Array<number>> = [Array.of(keyArr[0])];
        let xz3:Array<Array<number>> = [Array.of(keyArr[0])];
        let xz1Count = 0;
        let idx = 0;
        let xz2Count = 0;
        let xz3Count = 0;
        let len = keyArr.length;
        //获取所有的顺子
        for(let j = 1;j<len;j++){
            if(xz1[xz1Count][j-1-idx] !== keyArr[j] && (keyArr[j]-xz1[xz1Count][j-1-idx]===1)){
                xz1[xz1Count].push(keyArr[j]);
                if(mp.get(keyArr[j-1]) as number>=2 && mp.get(keyArr[j]) as number>=2){
                    xz2[xz2Count].push(keyArr[j]);
                }else {
                    if(xz2[xz2Count].length < 3){
                        xz2[xz2Count] = Array.of(keyArr[j]);
                    }else {
                        xz2Count++;
                        xz2[xz2Count] = Array.of(keyArr[j]);
                    }
                }

                if(mp.get(keyArr[j-1]) as number>=3 && mp.get(keyArr[j]) as number>=3){
                    xz3[xz3Count].push(keyArr[j]);
                }else {
                    if(xz3[xz3Count].length < 2){
                        xz3[xz3Count] = Array.of(keyArr[j]);
                    }else {
                        xz3Count++;
                        xz3[xz3Count] = Array.of(keyArr[j]);
                    }
                }
            }else {
                if(xz1[xz1Count].length >=5){
                    idx += xz1[xz1Count].length;
                    xz1Count++;
                    xz1[xz1Count] = Array.of(keyArr[j]);
                }else {
                    xz1[xz1Count] = Array.of(keyArr[j]);
                }

                if(xz2[xz2Count].length >=3){
                    xz2Count++;
                    xz2[xz2Count] = Array.of(keyArr[j]);
                }else {
                    xz2[xz2Count] = Array.of(keyArr[j]);
                }

                if(xz3[xz3Count].length >=2){
                    xz3Count++;
                    xz3[xz3Count] = Array.of(keyArr[j]);
                }else {
                    xz3[xz3Count] = Array.of(keyArr[j]);
                }
            }
        }
        //处理顺子返回最长的顺子
        let result1 = xz1.map(item=>{
            if(item[item.length-1]===13){
                if(keyArr.includes(1)){
                    item.push(1)
                }
            }
            return item
        }).reduce((pre:Array<number>,cur:Array<number>)=>{
            return pre.length > cur.length ? pre : cur
        },[])

        let result2 = xz2.map(item=>{
            if(item[item.length-1]===13){
                if(keyArr.includes(1)){
                    item.push(1)
                }
            }
            return item
        }).reduce((pre:Array<number>,cur:Array<number>)=>{
            return pre.length > cur.length ? pre : cur
        },[]).map(item=>[item,item]).flat()

        let result3 = xz3.map(item=>{
            if(item[item.length-1]===13){
                if(keyArr.includes(1)){
                    item.push(1)
                }
            }
            return item
        }).reduce((pre:Array<number>,cur:Array<number>)=>{
            return pre.length > cur.length ? pre : cur
        },[]).map(item=>[item,item,item]).flat()

        //校验一下
        let result:any = [];
        result1.length >= 5 ? result.push(result1) : '';
        result2.length >= 6 ? result.push(result2) : '';
        result3.length >= 6 ? result.push(result3) : '';
        //最终结果
        result = result.reduce((pre:Array<number>,cur:Array<number>)=>pre.length > cur.length ? pre : cur,[])
          
        console.log('result1',result1);
        console.log('result2',result2);
        console.log('result3',result3);
        
        return result.length > 0 ? result :false;
    }
    let arr = [1,1,1,3,3,3,2,2,2,5,4,4,7,8,9,10,12,11,11,11,12,12,13,13,13,15,15,15]
    console.log(getSz(arr));
}